package com.douqu.game.main.util;


import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.e.E_ServerType;
import com.douqu.game.core.e.ReturnMessage;
import com.douqu.game.core.entity.Level;
import com.douqu.game.core.entity.PlayerBattleData;
import com.douqu.game.core.entity.sprite.Player;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WordFactory;
import com.douqu.game.core.protobuf.*;
import com.douqu.game.core.web.response.BaseResponseDto;
import com.douqu.game.main.server.GameServer;
import com.douqu.game.main.server.ServerInfo;
import com.douqu.game.main.server.ServerManager;

/**
 * Created by bean on 2016/12/1.
 */
public class MsgUtils {

    public static BaseResponseDto createMsg(){

        return new BaseResponseDto(ReturnMessage.SUCCESS);
    }

    public static <T> BaseResponseDto createMsg(T data){

        return new BaseResponseDto(ReturnMessage.SUCCESS, data);
    }

    public static BaseResponseDto createMsg(ReturnMessage error){

        return new BaseResponseDto(error);
    }
//
//    public static HoodleProtoBuf.S2C_FlushData.Builder getFlushData(Config.E_RESOURCE_TYPE resourceType, Object value)
//    {
//        HoodleProtoBuf.S2C_FlushData.Builder response = HoodleProtoBuf.S2C_FlushData.newBuilder();
//        HoodleProtoBuf.ResourceData.Builder data = HoodleProtoBuf.ResourceData.newBuilder();
//        data.setResType(resourceType);
//        if(value instanceof String)
//            data.setStr(value.toString());
//        else if(value instanceof Integer)
//            data.setValue((Integer)value);
//
//        response.addResource(data);
//        return response;
//    }
//
//    public void addResourceData(HoodleProtoBuf.S2C_FlushData.Builder builder, Config.E_RESOURCE_TYPE resourceType, Object value)
//    {
//        HoodleProtoBuf.ResourceData.Builder data = HoodleProtoBuf.ResourceData.newBuilder();
//        data.setResType(resourceType);
//        if(value instanceof String)
//            data.setStr(value.toString());
//        else if(value instanceof Integer)
//            data.setValue((Integer)value);
//        builder.addResource(data);
//    }
//
//    public static void sendFlush(NettyConnection connection, SGPlayerProto.S2C_FlushAssetData.Builder response)
//    {
//        if(connection == null)
//            return;
//
//        connection.sendMsg(SGMainProto.E_MSG_ID.MsgID_Player_FlushData_VALUE, response.build().toByteArray());
//    }
//
//    public static void sendFlush(NettyConnection connection, E_RES resourceType, Object value)
//    {
//        if(connection == null)
//            return;
//
//        HoodleProtoBuf.S2C_FlushData.Builder response = HoodleProtoBuf.S2C_FlushData.newBuilder();
//        HoodleProtoBuf.ResourceData.Builder data = HoodleProtoBuf.ResourceData.newBuilder();
//        data.setResType(resourceType);
//        if(value instanceof String)
//            data.setStr(value.toString());
//        else if(value instanceof Integer)
//            data.setValue((Integer)value);
//
//        response.addResource(data);
//        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Player_FlushResData_VALUE, response.build().toByteArray());
//    }

//    public static void sendAlert(PlayerController playerController, SGCommonProto.E_NOTIFY_TYPE notifyResult)
//    {
//        if(playerController == null)
//            return;
//
//        SGSystemProto.S2C_NotifyAlert.Builder response = SGSystemProto.S2C_NotifyAlert.newBuilder();
//        response.setType(notifyResult);
//        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_System_NotifyAlert_VALUE, response.build().toByteArray());
//    }

//    public static void sendAlert(PlayerController playerController, SGCommonProto.E_NOTIFY_TYPE notifyResult)
//    {
//        if(connection == null)
//            return;
//
//        SGSystemProto.S2C_NotifyAlert.Builder response = SGSystemProto.S2C_NotifyAlert.newBuilder();
//        response.setType(notifyResult);
//        connection.sendMsg(SGMainProto.E_MSG_ID.MsgID_System_NotifyAlert_VALUE, response.build().toByteArray());
//    }

    /**

     * @param playerController
     * @param host
     * @param port
     * @param player
     * @param battleId
     */


    public static void sendArenaMatch(PlayerController playerController, String host, int port, Player player, String battleId)
    {
        SGPlayerProto.S2C_ArenaMatch.Builder response = SGPlayerProto.S2C_ArenaMatch.newBuilder();

        SGCommonProto.ServerInfo.Builder serverInfoData = SGCommonProto.ServerInfo.newBuilder();
        serverInfoData.setHost(host);
        serverInfoData.setPort(port);

        SGCommonProto.PlayerMatchInfo.Builder playerInfo = SGCommonProto.PlayerMatchInfo.newBuilder();
        SGCommonProto.PlayerBaseInfo.Builder baseInfo = SGCommonProto.PlayerBaseInfo.newBuilder();
        baseInfo.setPlayerIndex(player.getObjectIndex());
        baseInfo.setNickName(player.getName());
        baseInfo.setAvatar(player.getAvatar());
        baseInfo.setLv(player.getLv());
        playerInfo.setGrade(player.grade);
        playerInfo.setStar(player.star);
        playerInfo.setBaseInfo(baseInfo);

        response.setBattleId(battleId);
        response.setServerInfo(serverInfoData);
        response.setTargetInfo(playerInfo);

        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Player_ArenaMatch_VALUE, response.build().toByteArray());
    }


    public static void sendInstanceBattle(PlayerController playerController, int mapId, int levelId, String host, int port, String battleId)
    {
        SGInstanceProto.S2C_RequestLevelBattle.Builder response = SGInstanceProto.S2C_RequestLevelBattle.newBuilder();
        response.setMapId(mapId);
        response.setLevelId(levelId);

        SGCommonProto.ServerInfo.Builder serverInfoData = SGCommonProto.ServerInfo.newBuilder();
        serverInfoData.setHost(host);
        serverInfoData.setPort(port);

        response.setServerInfo(serverInfoData);
        response.setBattleId(battleId);

        playerController.sendMsg(SGMainProto.E_MSG_ID.MsgID_Instance_RequestLevelBattle_VALUE, response.build().toByteArray());
    }


    public static void startBattle(PlayerController playerController, Object target, SGCommonProto.E_BATTLE_TYPE battleType)
    {
        ServerInfo serverInfo = ServerManager.getInstance().getFreeServer(E_ServerType.TCP_BATTLE);
        if(serverInfo == null)
        {
            System.out.println("战斗服务器没有开启!");
            playerController.sendAlert(SGCommonProto.E_NOTIFY_TYPE.NOTIFY_TYPE_WARN, DataFactory.getInstance().getWord(WordFactory.BATTLE_SERVER_ERROR));
        }
        else
        {
            String battleId = GameServer.getInstance().createBattleId();

            PlayerBattleData battleData = new PlayerBattleData(battleId, battleType, serverInfo.getIp(), serverInfo.getPort());
            playerController.setBattleData(battleData);

            if(target instanceof PlayerController)
            {
                PlayerController targetController = (PlayerController) target;

                serverInfo.sendBattleInit(battleId, battleType.getNumber(), playerController.getObjectIndex(), targetController.getObjectIndex());

                targetController.setBattleData(battleData);

                MsgUtils.sendArenaMatch(playerController, serverInfo.getIp(), serverInfo.getPort(), targetController.getPlayer(), battleId);
                MsgUtils.sendArenaMatch(targetController, serverInfo.getIp(), serverInfo.getPort(), playerController.getPlayer(), battleId);
            }
            else if(target instanceof Player)
            {
                Player player = (Player) target;
                serverInfo.sendBattleInit(battleId, battleType.getNumber(), playerController.getObjectIndex(), player.getObjectIndex());

                MsgUtils.sendArenaMatch(playerController, serverInfo.getIp(), serverInfo.getPort(), player, battleId);
            }
            else if(target instanceof Level)
            {
                Level level = (Level) target;

                serverInfo.sendBattleInit(battleId, battleType.getNumber(), playerController.getObjectIndex(), level.getId()+"");

                MsgUtils.sendInstanceBattle(playerController, level.getInstanceId(), level.getId(), serverInfo.getIp(), serverInfo.getPort(), battleId);
            }

        }
    }


}
